package com.dgshanger.sanhxiaofeisc.ui.person

import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.AppApi
import com.dgshanger.sanhxiaofeisc.entity.PromoteRankingEntity
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ImageLoader
import com.xx.baseuilibrary.adapter.BaseRecycleAdapter
import com.xx.baseuilibrary.adapter.BaseViewHolder
import com.xx.baseuilibrary.mvp.contract.BaseMvpRecycleViewContract
import com.xx.baseuilibrary.mvp.lcec.BaseMvpRecycleViewFragment
import com.xx.baseutilslibrary.network.rx.RxHelper
import com.xx.baseutilslibrary.network.rx.XxBaseHttpObserver
import kotlinx.android.synthetic.main.item_promote_ranking_list.view.*


/**
 * PromoteRankingListFragment
 * 沉迷学习不能自拔
 * Describe：
 * Created by 雷小星🍀 on 2018/4/18 16:28.
 */
class PromoteRankingListFragment : BaseMvpRecycleViewFragment<PromoteRankingEntity>() {

    var type: String = PromoteRankingActivity.TYPE_MONTH//默认为日排行
        set(value) {
            //设置之后刷新状态
            field = value
            contentView.post {
                contentView.isRefreshing = true
            }
            loadData(true)
        }

    override val listLimit: Int
        get() = 10
    override val layoutManager: RecyclerView.LayoutManager
        get() = LinearLayoutManager(mContext)

    override fun createAdapter(): BaseRecycleAdapter<PromoteRankingEntity, out BaseViewHolder<PromoteRankingEntity>> = Adapter()

    override fun createModel(): BaseMvpRecycleViewContract.Model<PromoteRankingEntity> = Model()


    inner class Model : BaseMvpRecycleViewContract.Model<PromoteRankingEntity> {
        override fun loadData(page: Int, httpObserver: XxBaseHttpObserver<List<PromoteRankingEntity>>?) {
            AppApi.Api()
                    .recommend_rank(page, type)
                    .compose(RxHelper.io_main())
                    .compose(RxHelper.start_finish(httpObserver))
                    .subscribe(httpObserver)
        }

    }

    private class Adapter : BaseRecycleAdapter<PromoteRankingEntity, ViewHolder>() {

        override fun createViewHolder(view: View?, viewType: Int): ViewHolder = ViewHolder(view!!)

        override fun getItemLayout(viewType: Int): Int = R.layout.item_promote_ranking_list
    }

    private class ViewHolder(view: View) : BaseViewHolder<PromoteRankingEntity>(view) {
        override fun bindView(position: Int, entity: PromoteRankingEntity?) {
            itemView.tv_number.text = (position + 1).toString()
            with(entity!!) {
                itemView.tv_nick_name.text = nick_name
                itemView.tv_count.text = count
                ImageLoader.loadAvatar(itemView.context, itemView.iv_avatar, avatar)
            }
        }
    }

}